﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Linq.Dynamic;
using System.Web.Mvc;
using SDT.Model;
using SDT.Lib.Common;
using SDT.Admin.Properties;
using SDT.Admin.ViewModels;
using SDT.Lib.Paging;
using System.Web.UI.WebControls;
using System.IO;
using System.Data.Linq;
using SDT.Lib.Helpers;

namespace SDT.Admin.Controllers
{
    [RoleHelper(Roles = RoleSystem.Dictionary)]
    public class CountryController : Controller
    {
        //
        // GET: /Country/
        SDTDataContext db = new SDTDataContext();
        public ActionResult Index()
        {
            var c = db.Countries.OrderBy(e => e.CountryName).ToList();
            return View(c);
        }

        //
        // GET: /Country/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Country/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Insurer/Create

        [HttpPost]
        public ActionResult Create(Country p)
        {
            try
            {
                // TODO: Add insert logic here
                UpdateModel(p);
                db.Countries.InsertOnSubmit(p);
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Insurer/Edit/5

        public ActionResult Edit(int id)
        {
            var p = db.Countries.Where(e=>e.ID==id).SingleOrDefault();
            return View(p);
        }

        //
        // POST: /Insurer/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Country p)
        {
            try
            {
                p = db.Countries.Where(e => e.ID == id).SingleOrDefault();
                UpdateModel(p);
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Insurer/Delete/5

        public ActionResult Delete(int id)
        {
            var p = db.Countries.Where(e => e.ID == id).SingleOrDefault();
            return View(p);
        }

        //
        // POST: /Insurer/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                var p = db.Countries.Where(e => e.ID == id).SingleOrDefault();
                if(p!=null ){
                    db.Countries.DeleteOnSubmit(p);
                    db.SubmitChanges();
                }
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
